package user

import (
	"wx-uniapp-go/internal/domain/errcode"
	"wx-uniapp-go/internal/logic/points_logic"
	"wx-uniapp-go/internal/repository/dto"
	"wx-uniapp-go/internal/server/echo"

	"github.com/gin-gonic/gin"
)

// ChangePoints 变更积分
func ChangePoints(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	req := new(dto.PointsChangeReq)
	if err := c.ShouldBindJSON(&req); err != nil {
		echo.Fail(c).Error(errcode.ParametersErr)
		return
	}

	if err := points_logic.NewPointsLogic(ctx).ChangePoints(req); err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c)
}

// GetUserPoints 获取用户积分
func GetUserPoints(c *gin.Context) {
	ctx := echo.TraceCtx(c)
	userId := c.GetInt64("user_id") // 从登录态获取用户ID

	resp, err := points_logic.NewPointsLogic(ctx).GetUserPoints(userId)
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c, resp)
}

// GetPointsRules 获取积分规则
func GetPointsRules(c *gin.Context) {
	ctx := echo.TraceCtx(c)

	rules, err := points_logic.NewPointsLogic(ctx).GetPointsRules()
	if err != nil {
		echo.Fail(c).Error(err)
		return
	}

	echo.Success(c, rules)
}
